﻿using System.Threading;
using MbUnit.Framework;
using Microsoft.SharePoint;

namespace CodePlex.Lekman.SharePoint.CacheDependencies.Tests
{
    /// <summary>
    /// Contains unit tests for the <see cref="SPListCacheDependency"/> class.
    /// </summary>
    [TestFixture]
    public class SPListCacheDependencyTestFixture : TestFixtureBase
    {
        /// <summary>
        /// Ensures that the class can be instantiated.
        /// </summary>
        [Test]
        public void TestInstantiate()
        {
            Assert.IsNotNull(new SPListCacheDependency(Web.Lists[0]));
        }

        /// <summary>
        /// Tests that the changed event fires when the list properties has changed.
        /// </summary>
        [Test]
        public void TestPropertiesHasChanged()
        {
            SPList list = Web.Lists[0];
            string title = list.Title;
            try
            {
                SPListCacheDependency dependency = new SPListCacheDependency(list,
                                                                             true,
                                                                             false,
                                                                             1000);

                //Now change the title property of the list
                list.Title = "Changed!";
                list.Update();

                //Wait for the dependency to detect the change
                Thread.Sleep(10000);
                Assert.IsTrue(dependency.HasChanged);
            }
            finally
            {
                list.Title = title;
                list.Update();
            }
        }
    }
}